<?php defined('SYSPATH') or die('No direct script access.');
/**
 * 管理员认证封装
 *
 * @author  Yellow.Chow <aultoale@gmail.com>
 * @version $Id: manager.php 1 2014-01-13 15:05:51Z zhouyl $
 * @license http://www.gnu.org/licenses/gpl-3.0.html    GPL 3
 */
class Manager
{

    /**
     * 管理员认证类单件实例
     *
     * @var Manager
     */
    protected static $_instance = NULL;

    /**
     * Auth 会员认证类
     *
     * @var Auth
     */
    protected $_auth = NULL;

    /**
     * Manager_Role 角色类实例
     *
     * @var Manager_Role
     */
    protected $_role = NULL;

    /**
     * Session 存储
     *
     * @var Session_NameSpace
     */
    protected $_session = NULL;

    /**
     * 单件模式调用
     *
     * @return Manager
     */
    public static function instance()
    {
        if (self::$_instance === NULL)
        {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

    /**
     * 构造方法
     */
    protected function __construct()
    {
        $this->_auth = Auth::instance();
        $this->_role = new Manager_Role();
        $this->_session = Session_NameSpace::factory(__CLASS__);
    }

    /**
     * 禁止使用 clone 方法
     */
    final protected function __clone()
    {}

    /**
     * 会员登录
     *
     * @param  string  $account   邮箱/昵称
     * @param  string  $password  登录密码(未加密)
     * @param  int     $lifetime  保存密码的周期
     * @return stdClass
     */
    public function login($account, $password, $lifetime = FALSE)
    {
        $this->_auth->login($account, $password, $lifetime);

        $this->_comple_login();
    }

    /**
     * 完成登录信息
     *
     * @throws Manager_Exception
     */
    protected function _comple_login()
    {
        $role = $this->_role->get($this->_auth->member_id);

        if ( ! $role)
        {
            throw new Manager_Exception('对不起，您的身份不是管理员');
        }

        $this->_session->set($role);

        $this->_session->set('member_id', $this->_auth->member_id);
    }

    /**
     * 刷新 SESSION 数据
     */
    public function refresh()
    {
        if ($this->is_logined())
        {
            $this->_auth->refresh();
            $this->_role->flush();
            $this->_comple_login();
        }
    }

    /**
     * 退出登录
     *
     * @return Auth
     */
    public function logout()
    {
        $this->_session->clean();
    }

    /**
     * 判断是否登录
     *
     * @return bool
     */
    public function is_logined()
    {
        return ($this->_auth->is_logined() AND $this->_session->role_id AND
               ($this->_auth->member_id === $this->_session->member_id));
    }

    /**
     * 获取已经存储的用户信息
     *
     * @param  string  $key
     * @return string
     */
    public function __get($key)
    {
        return $this->_session->get($key, $this->_auth->$key);
    }

    /**
     * 获取所有的用户信息
     *
     * @return array
     */
    public function as_array()
    {
        return array_merge($this->_auth->as_array(), $this->_session->as_array());
    }

    /**
     * 判断集合中是否有一个拥有权限
     *
     * @param  array  $privileges
     * @return bool
     */
    public function has_allow(array $privileges)
    {
        if ($this->is_logined() !== TRUE)
        {
            return FALSE;
        }

        foreach ($privileges as $privilege)
        {
            if ($this->_role->is_allow($this->_session->role_id, $privilege))
            {
                return TRUE;
            }
        }

        return FALSE;
    }

    /**
     * 判断是否拥有访问权限
     *
     * @param  string|array  $privileges
     * @return bool
     */
    public function is_allow($privileges)
    {
        if ($this->is_logined() !== TRUE)
        {
            return FALSE;
        }

        if ( ! is_array($privileges))
        {
            $privileges = array($privileges);
        }

        foreach ($privileges as $privilege)
        {
            if ( ! $this->_role->is_allow($this->_session->role_id, $privilege))
            {
                return FALSE;
            }
        }

        return TRUE;
    }

}